/*********************************************************************************
 *      Copyright:  (C) 2022 Li Yanting<liyanting2@163.com>
 *                  All rights reserved.
 *
 *       Filename:  client_sqlite.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/19/2022)
 *         Author:  Li Yanting <liyanting2@163.com>
 *      ChangeLog:  1, Release initial version on "04/19/2022 12:47:46 PM"
 *                 
 ********************************************************************************/

#include "server_sqlite.h"

/*********************************************************************************
 *      function name:  create_database
 *        Description:  Create a database.
 *       return value:  On error, -1 is returned. On success, 0 is returned.
 *          parameter:  Address of db.
 ********************************************************************************/
int create_database(sqlite3 **db)
{
    char           *zerrmsg = 0;
    int             rc;

    rc = sqlite3_open("data.db", db);

    if(rc != SQLITE_OK )
    {
		dbg_printf("create_database: %s\n", zerrmsg);
        
		return -1;
    }		
	dbg_printf("create_database successfully.\n");

	return 0;
}

/*********************************************************************************
 *      function name:  create_statement
 *        Description:  create a statement.
 *       return value:  On error, -1 is returned. On success, 0 is returned.
 *          parameter:  db; statement name.
 ********************************************************************************/
int create_statement(sqlite3 *db, char *host_number)
{
    char       buffer[256];
    char       *zerrmsg = 0;
	char       *a = "(HOST INT NOT NULL, TIME CHAR(50) NOT NULL, TEMPER FLOAT NOT NULL);";

	sprintf(buffer, "CREATE TABLE %s %s", host_number, a);
    
    if ( sqlite3_exec(db, buffer, 0, 0, &zerrmsg) != SQLITE_OK )
    {
		dbg_printf("create_statement: %s\n", zerrmsg);
        return -1;
    }
	dbg_printf("create statement[%s] successfully.\n", host_number);

	return 0;
}

/*********************************************************************************
 *      function name:  sql_insert
 *        Description:  insert data to statement
 *       return value:  On error, -1 is returned. On success, 0 is returned.
 *          parameter:  db; statement name; current format time; temper.
 ********************************************************************************/
int sql_insert(sqlite3 *db, char *host_number, char *format_time, char *temper)
{
    char        buffer[128];
    char       *insert_err;

    sprintf(buffer, "INSERT INTO %s VALUES ('%s', '%s', '%s')", host_number, host_number, format_time, temper);

    if( sqlite3_exec(db, buffer, 0, 0, &insert_err) != SQLITE_OK )
    {
		dbg_printf("insert data to %s failure: %s\n", host_number, insert_err);
        return -1;
    }
	dbg_printf("insert data[%s %s %s]to %s successful\n", host_number, format_time, temper, host_number);

    return 0;
}





